﻿// /*
//  * @Author: wanyiming
//  * @Date:2024-06-13-21:56
//  * @Description:
//  */

using System;
using System.Collections.Generic;
using Code.GameLogic.BaseClass;
using Code.GameLogic.XianJian.UI;
using Code.GameLogic.XianJian.XianJianBehavior;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Serialization;

namespace Code.GameLogic.XianJian.Controller
{
    public class XianJianSpaceController:AbsGameLogicComponent<XianJianController>
    {
        private SpriteRenderer _xianJianSpaceSpriteRenderer;
        private XianJianBo _xianJianBo;

        public List<GameObject> scopeObjects;
        public GameObject lockingObject;
        
        public override void InitComponent()
        {
            
            _xianJianBo = ThisController.bo;
            transform.parent = ThisController.transform;
            transform.localPosition=Vector3.zero;
            _xianJianSpaceSpriteRenderer = this.AddComponent<SpriteRenderer>();
            _xianJianSpaceSpriteRenderer.sprite= Resources.Load<Sprite>("地图/建筑物/范围指示器");
            _xianJianSpaceSpriteRenderer.sortingOrder = 3;
            Color tempColor1 = _xianJianSpaceSpriteRenderer.color;
            tempColor1.a = 0.0f;
            _xianJianSpaceSpriteRenderer.color = tempColor1;
            
            scopeObjects = new List<GameObject>();
            
            BoxCollider2D boxCollider2D = this.AddComponent<BoxCollider2D>();
            boxCollider2D.size = new Vector2(ThisController.bo.MainBehavior.Area, ThisController.bo.MainBehavior.Area);
            boxCollider2D.isTrigger = true;

        }
        
        
        
        public void MouseShow(bool isShow)
        {
            if (isShow)
            {
                _xianJianSpaceSpriteRenderer.sortingOrder = 3;
                Color tempColor = _xianJianSpaceSpriteRenderer.color;
                tempColor.a = 0.5f;
                _xianJianSpaceSpriteRenderer.color = tempColor;
                var xianJianBoActArea = (_xianJianBo.MainBehavior.Area + 1) * 3;
                transform.localScale = new Vector2(xianJianBoActArea, xianJianBoActArea);
            }
            else
            {
                _xianJianSpaceSpriteRenderer.sortingOrder = -3;
            }
        }
        
        
        void OnTriggerEnter2D(Collider2D  collision){
            if (collision.gameObject.CompareTag("Monster"))
            {
                InSpaceObj(collision.gameObject);
            }
        }


        void OnTriggerExit2D(Collider2D  collision){
            if (collision.gameObject.CompareTag("Monster")){
                OutSpaceObj(collision.gameObject);
            }
        }

        private void OnTriggerStay2D(Collider2D collision)
        {
        }
        
        private void InSpaceObj(GameObject collision)
        {
            lockingObject ??= collision;
            //添加到目标
            scopeObjects.Add(collision);
        }

        private void OutSpaceObj(GameObject collision)
        {
            if (lockingObject == collision) lockingObject = null;
            //移除到目标
            scopeObjects.Remove(collision);
        }

        
    }
}